#include "trianglemesh.h"
#include <QFile>
#include <QTextStream>
#include <QStringList>
#include <QDebug>

TriangleMesh::TriangleMesh(QObject *parent) :
    RTObject(parent)
{
    this->readContent();
}

TriangleMesh::~TriangleMesh()
{

}

void TriangleMesh::calcBoundingBox()
{
    min[0] = vertices[0][0];
    min[1] = vertices[0][1];
    min[2] = vertices[0][2];
    max[0] = vertices[0][0];
    max[1] = vertices[0][1];
    max[2] = vertices[0][2];

    for(int i = 1; i < vertexCount; ++i)
    {
        for(int j=0; j < 3; j++)
        {
            if(min[j]>vertices[i][j])
            {
                min[j] = vertices[i][j];
            }

            if(max[j]<vertices[i][j])
            {
                max[j] = vertices[i][j];
            }
        }
    }

}

void TriangleMesh::readContent()
{

    // read from file


    QFile m3u( ":/dreieck1.dat" );

    m3u.open(QIODevice::ReadOnly);
    QTextStream fileStream(&m3u);

    if(this->readLine(fileStream) == "TRIANGLE_MESH")
    {

        QString valueString = this->readLine(fileStream);
        QStringList list;
        list = valueString.split(QRegExp("\\s+"));
        this->vertexCount = list.at(0).toInt();
        this->triangleCount = list.at(1).toInt();
        this->materialCount = list.at(2).toInt();
        this->fgp =list.at(3).toInt();


        allocateArrays();
        for (int i =0; i <materialCount; i++)
        {
            QStringList materialData = readLine(fileStream).split(QRegExp("\\s+"));
            for (int n = 0; n < 9; n++)
            {
                materials[i][n] = materialData.at(n).toFloat();
                qDebug()<<"material ["<<i<<"]  ["<<n<<"]"<<materialData.at(n);
            }
            materialsN[i] = materialData[9].toInt();
        }
        for (int i =0; i < vertexCount; i++)
        {
            QStringList vertexData = readLine(fileStream).split(QRegExp("\\s+"));
            for (int n = 0; n < 3; n++)
            {
                vertices[i][n] = vertexData.at(n).toFloat();
                qDebug()<<"triangle ["<<i<<"]  ["<<n<<"]"<<vertexData.at(n);
            }
        }

        for (int i =0; i < triangleCount; i++)
        {
            QStringList triangleData = readLine(fileStream).split(QRegExp("\\s+"));
            for (int n = 0; n < 3; n++)
            {
                triangles[i][n] = triangleData[n].toInt();

            }
        }
    }
}

void TriangleMesh::allocateArrays()
{
    materials = new float*[materialCount];
    for (int i = 0; i < materialCount; i++)
    {
        materials[i] = new float[9];
    }

    materialsN = new int[materialCount];

    vertices = new float*[vertexCount];
    for (int i = 0; i < vertexCount; i++)
    {
        vertices[i] = new float[3];
    }

    verticesMaterial = new int[vertexCount];

    triangles = new int*[triangleCount];
    for (int i = 0; i < triangleCount; i++)
    {
        triangles[i] = new int[3];
    }
}
